I can translate the text for you: 如何在Ionic 7上从手机存储播放声音/音频?

huangapple go评论70阅读模式
英文:

How to play sound/audio from phone storage on ionic 7?

问题

I'll provide a translation of the code portion:

const url = await Filesystem.getUri({
    directory: Directory.Documents,
    path: 'test.ogg',
})

window.plugins.NativeAudio.preloadSimple('test', url.uri, function (msg) {
}, function (msg) {
    console.log('error: ' + msg);
});

window.plugins.NativeAudio.play('test');

If you need further assistance, feel free to ask.

英文:

How to play sound/audio from phone storage on ionic 7?

I tried using cordova-plugin-nativeaudio like below

const url = await Filesystem.getUri({
    directory: Directory.Documents,
    path: 'test.ogg',
})

window.plugins.NativeAudio.preloadSimple('test', url.uri, function (msg) {
}, function (msg) {
    console.log('error: ' + msg);
});

window.plugins.NativeAudio.play('test');

and it's returns:

Msg: error: java.io.FileNotFoundException: file:///storage/emulated/0/Documents/test.ogg

If the path is from my app public path it works. But if it from phone storage it doesn't work.

On the documentation says:

> assetPath - the relative path or absolute URL (inluding http://) to
> the audio asset.

But why I tried absolute URL of phone storage still fail? I already give storage permission to my app.

答案1

得分: 1

I found this answer that seems cordova-plugin-nativeaudio is not work with phone storage. But I know the work around just using HTML5 audio:

import {Filesystem, Directory} from '@capacitor/filesystem';
import {Capacitor} from '@capacitor/core';

const url = await Filesystem.getUri({
    directory: Directory.Documents,
    path: 'secrets/test.ogg',
})

const fileSrc = Capacitor.convertFileSrc(url.uri);
const audio = new Audio(fileSrc);
audio.play();

Actually I tried new Audio(url.uri) but doesn't work (file permission issue), after I convert that path using convertFileSrc() method first, it works.

英文:

I found this answer that seems cordova-plugin-nativeaudio is not work with phone storage. But I know the work around just using HTML5 audio:

import {Filesystem, Directory} from '@capacitor/filesystem';
import {Capacitor} from '@capacitor/core';

const url = await Filesystem.getUri({
    directory: Directory.Documents,
    path: 'secrets/test.ogg',
})

const fileSrc = Capacitor.convertFileSrc(url.uri);
const audio = new Audio(fileSrc);
audio.play();

Actually I tried new Audio(url.uri) but doesn't work(file permission issue), after I convert that path using convertFileSrc() method first, it works.

huangapple
  • 本文由 发表于 2023年4月11日 07:40:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/75981485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定