Mp3 / Mp4播放,但同时没有声音

时间:2018-01-26 22:30:02

标签: android mp3 mp4

我写了一个简单的媒体播放器。正如您所看到的,它会检查文件是否存在,并根据此情况播放mp3或mp4。首先,几次,完全没有问题。但是,突然,mp3或mp4正在播放(我可以看到这一点),同时,没有声音。重新启动应用程序时,此问题已得到解决。谢谢你的帮助......

public class Media_Player extends Activity {

    MediaPlayer sound_play;
    private VideoView vidView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.media_player_layout);

    vidView = (VideoView) findViewById(R.id.vidView);

    play_media("media_name");
}

private void play_media(String file_name) {

    String path1 = Environment.getExternalStorageDirectory().getPath() + "/Media/" + file_name + ".mp3";
    File file1 = new File(path1);
    String path2 = Environment.getExternalStorageDirectory().getPath() + "/Media/" + file_name + ".mp4";
    File file2 = new File(path2);

        if (file1.exists()) {
            sound_play = MediaPlayer.create(getApplicationContext(), Uri.parse(path1));
            sound_play.start();
        } 

        if (file2.exists()) {
            video_play(path2);
        }
}


private void video_play(String path) {
    Uri video = Uri.parse(path);
    vidView.setVideoURI(video);
    vidView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
        @Override
        public void onPrepared(MediaPlayer mp) {
            vidView.start();
        }
    });
}
}

0 个答案:

没有答案