简体   繁体   English

Android:SD卡/内部存储中的.mp3无法播放

[英]Android: .mp3 from sdcard/internal storage not playing

I'm having a bit of trouble playing a .mp3 file from my phone's internal storage. 从手机的内部存储播放.mp3文件时遇到麻烦。 I'm working on this app for a Course I'm following, but I can't seem to figure out why my .mp3 files won't play. 我正在为正在关注的课程开发此应用,但似乎无法弄清为什么无法播放.mp3文件。

Here's the basic setup of the app: 这是应用程序的基本设置:

I have to create a screen with 4 buttons; 我必须创建一个带有4个按钮的屏幕; three that are linked to .mp3 files on the local storage, and one to open up a browser that lets you browse for .mp3 files. 三个链接到本地​​存储上的.mp3文件,一个打开浏览器,让您浏览.mp3文件。 On the browse screen, when a file is selected and it's a .mp3 , three buttons appear to let you define a new link so the audio can be played. 在浏览屏幕上,当一个文件被选择为.mp3时 ,将出现三个按钮,您可以定义一个新的链接来播放音频。

I am using the file/folder browsing from here: http://custom-android-dn.blogspot.nl/2013/01/create-simple-file-explore-in-android.html , which works perfectly. 我正在使用从此处浏览文件/文件夹: http : //custom-android-dn.blogspot.nl/2013/01/create-simple-file-explore-in-android.html ,效果很好。 I can pick up my .mp3 files without a problem, all the way up to the base-path of my internal storage. 我可以毫无问题地拾起.mp3文件,一直到内部存储的基本路径。

I then looked at how I should play audio-files from my storage, here: Play mp3 sounds from SD card (the accepted answer) , though I had to tweak it a little, as the file-path was already defined in a different Class. 然后,我查看了如何从存储中播放音频文件,这里: 播放SD卡中的mp3声音 (公认的答案) ,尽管我不得不稍作调整,因为文件路径已经在其他类中定义了。

The code I'm using (class-names defined after the description).. 我正在使用的代码(在描述后定义的类名)。

..To play the audio ( MainActivity.java ): ..播放音频( MainActivity.java ):

if(R.id.mp3_1_btn == b.getId()){
    a.PlayAudio(0);
}

..Function PlayAudio()( Audio.java ) ..Function PlayAudio()( Audio.java

public void PlayAudio(int audioFile){
    mp.setAudioStreamType(AudioManager.STREAM_MUSIC);

    if(AudioArray[audioFile] != ""){
        if(mp != null){
            mp.release();
        }

        try{
            FileDescriptor fd =null;
            FileInputStream fis = new FileInputStream(AudioArray[audioFile]);
            fd = fis.getFD();

            if(fd != null){
                mp.setDataSource(fd);
                mp.prepare();
                mp.start();
                Message("Play");
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

..The onCreate of the Browser ( FileExplorerActivity.java ): ..浏览器的onCreate( FileExplorerActivity.java ):

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    currentDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
    fill(currentDir); 
}

..The assigning of audio files to a button ( FileExplorerActivity.java ): ..将音频文件分配给按钮( FileExplorerActivity.java ):

if(R.id.AssignBtn1 == b.getId()){
    a.setAudioString(0, fullDirectory);
    this.finish();
}

..Things related to fullDirectory ( FileExplorerActivity.java/FileBrowser.java ) ..与fullDirectory相关的事物FileExplorerActivity.java/FileBrowser.java

fullDirectory = data.getStringExtra("GetPath")+"/"+data.getStringExtra("GetFileName");
intent.putExtra("GetPath",currentDir.toString());
intent.putExtra("GetFileName",o.getName());

..Things related to AudioArray ( Audio.java ) ..与AudioArrayAudio.java )相关的事物

private static String AudioArray[] = {"", "", ""};
public void setAudioString(int index, String dir){
    AudioArray[index] = dir;
}

And here's the output string from my fullDirectory variable: 这是我的fullDirectory变量的输出字符串:

/storage/emulated/0/Music/<name>.mp3

I've also got some logs from any errors and messages. 我还从任何错误和消息中获得了一些日志。

When I assign a .mp3 to a button 当我将.mp3分配给按钮时

08-24 22:44:11.468: D/MediaPlayer(10327): release() in
08-24 22:44:11.468: D/MediaPlayer(10327): release() out
08-24 22:44:11.478: W/System.err(10327): java.lang.IllegalStateException
08-24 22:44:11.478: W/System.err(10327):    at     android.media.MediaPlayer._setDataSource(Native Method)
08-24 22:44:11.478: W/System.err(10327):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1282)
08-24 22:44:11.478: W/System.err(10327):    at android.media.MediaPlayer.setDataSource(MediaPlayer.java:1254)
08-24 22:44:11.478: W/System.err(10327):    at com.example.mp3.Audio.PlayAudio(Audio.java:32)
08-24 22:44:11.478: W/System.err(10327):    at com.example.mp3.MainActivity.onClick(MainActivity.java:53)
08-24 22:44:11.478: W/System.err(10327):    at android.view.View.performClick(View.java:4480)
08-24 22:44:11.478: W/System.err(10327):    at android.view.View$PerformClick.run(View.java:18686)
08-24 22:44:11.478: W/System.err(10327):    at android.os.Handler.handleCallback(Handler.java:733)
08-24 22:44:11.478: W/System.err(10327):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-24 22:44:11.478: W/System.err(10327):    at android.os.Looper.loop(Looper.java:157)
08-24 22:44:11.488: W/System.err(10327):    at android.app.ActivityThread.main(ActivityThread.java:5872)
08-24 22:44:11.488: W/System.err(10327):    at java.lang.reflect.Method.invokeNative(Native Method)
08-24 22:44:11.488: W/System.err(10327):    at java.lang.reflect.Method.invoke(Method.java:515)
08-24 22:44:11.488: W/System.err(10327):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
08-24 22:44:11.488: W/System.err(10327):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
08-24 22:44:11.488: W/System.err(10327):    at dalvik.system.NativeStart.main(Native Method)

When trying to play said .mp3 (this crashes the app, obviously) 尝试播放说.mp3时(显然,这使应用程序崩溃了)

08-24 22:45:21.628: W/dalvikvm(10327): threadid=1: thread exiting with uncaught exception (group=0x4170ee18)
08-24 22:45:21.628: E/AndroidRuntime(10327): FATAL EXCEPTION: main
08-24 22:45:21.628: E/AndroidRuntime(10327): Process: com.example.mp3, PID: 10327
08-24 22:45:21.628: E/AndroidRuntime(10327): java.lang.IllegalStateException
08-24 22:45:21.628: E/AndroidRuntime(10327):    at android.media.MediaPlayer._setAudioStreamType(Native Method)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at android.media.MediaPlayer.setAudioStreamType(MediaPlayer.java:2135)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at com.example.mp3.Audio.PlayAudio(Audio.java:18)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at com.example.mp3.MainActivity.onClick(MainActivity.java:53)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at android.view.View.performClick(View.java:4480)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at android.view.View$PerformClick.run(View.java:18686)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at android.os.Handler.handleCallback(Handler.java:733)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at android.os.Looper.loop(Looper.java:157)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at android.app.ActivityThread.main(ActivityThread.java:5872)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at java.lang.reflect.Method.invokeNative(Native Method)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at java.lang.reflect.Method.invoke(Method.java:515)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
08-24 22:45:21.628: E/AndroidRuntime(10327):    at dalvik.system.NativeStart.main(Native Method)

So, we finally come to the question: 因此,我们终于想到了一个问题:

What is wrong with my code, and how do I fix it? 我的代码有什么问题,如何解决? Should I try a different approach, or did I miss something? 我应该尝试其他方法还是错过了什么?

I hope someone can help me with this. 我希望有人可以帮助我。 And if someone wants me to put my project on Dropbox and post a link, be sure to ask and I'll gladly comply. 如果有人要我将我的项目放到Dropbox上并发布链接,请务必提出要求,我会很乐意遵守。

Another approach is an audio service: 另一种方法是音频服务:

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;

public class AudioService extends Service {
    MediaPlayer _player;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        _player = MediaPlayer.create(this, R.raw.someaudio);
        //where someaudio (someaudio.mp3) is the raw resource id for the resource to use as the datasource

        // or _player = MediaPlayer.create(this, ... uri ...);
        // where uri is the Uri from which to get the datasource

        // see http://developer.android.com/reference/android/media/MediaPlayer.html

        _player.setLooping(false);
    }

    @Override
    public void onDestroy() {
        if (_player.isPlaying()) {
            _player.stop();
        }

        _player.release();
        _player = null;
    }

    @Override
    public void onStart(Intent intent, int startid) {
        _player.start();
    }
}

To call the service in a MainActivity: 要在MainActivity中调用服务:

startService(new Intent(this, AudioService.class)); // start service to play mp3 file

// to stop playing: stopService(new Intent(this, AudioService.class));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM