MediaPlayer无法正确播放音频 [英] MediaPlayer not playing audio properly

查看:231
本文介绍了MediaPlayer无法正确播放音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从WebView后台播放音频.该音频由URL给出.我通过重写url加载来解决这个问题.它开始播放音频,但是很多时候Media Player都停止了.这种情况发生的时间约为30%,并且此音频文件的播放时间永远不会超过30秒.

I'm trying to play an audio in background from a WebView. This audio is given by a URL. I approached this by overriding the url loading. And it starts playing the audio, but many times the Media Player just stops. This happens around 30% of times, and this audio files are never longer than 30 seconds.

我尝试了MP3,OGG和WAV,并且它们中的任何一个都发生了.

I tried with MP3, OGG and WAV, and it happens with any of them.

我也尝试过但先下载文件然后播放它,而不是流式传输它,但是也不起作用.

I also tried but first downloading the file and then playing it, instead of stream it, but doesn't work either.

这是一段代码...向您展示其工作原理:

This is a piece of code... to show you how it works:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.endsWith(".ogg")){
        AudioLoader audioLoader = new AudioLoader(url);
        audioLoader.start();
        return true;
    }
    else if (url.endsWith(".wav")){
        Uri tempPath = Uri.parse(url);
        MediaPlayer player = MediaPlayer.create(interfazWeb, tempPath);
        if (player != null){
            player.start();
        } else {
            Log.e(TAG, "No se puede abrir el audio:" + url);
        }
        return true;
    }
    else if (url.endsWith(".mp3")){
        AudioLoader audioLoader = new AudioLoader(url);
        audioLoader.start();
        return true;
    }else{
        return super.shouldOverrideUrlLoading(view, url);
    }
}

我已经检查了"AudioLoader"保存的音频文件,它完全可以. WAV案例正在使用我的第一次尝试,并通过流媒体播放它.

I've checked the audio file saved by "AudioLoader", and it's totally fine. And the WAV case is using my first attemp, play it with streaming.

还尝试了SoundPool和AsyncPlayer ...什么都行不通!!

Also tried SoundPool and AsyncPlayer... nothing works!!

所以...到目前为止,我认为这不是通信,编解码器或缓冲区问题.我唯一的提示是这些日志条目,使用所有格式和所有方法,每次发生问题时都会重复该日志条目:

So... this far I don't think is a communication, codec or buffer issue. My only tips are these log entries, which repeats for every time tha the problem happens, with all formats and all approaches:

12-31 09:41:49.284: WARN/AudioFlinger(59): write blocked for 160 msecs, 20 delayed writes, thread 0xd7a8
12-31 09:41:49.554: WARN/TimedEventQueue(59): Event 6 was not found in the queue, already cancelled?

有人请提供一些线索吗?或者我正面临错误/缺失功能.

Does anybody please have some clue? Or I'm just facing a bug/missfunction.

度过一个快乐的2011年,特别是如果您能够帮助我:P

Have a happy 2011, specially if you're able to help me :P

问候,曼努埃尔.

推荐答案

您正在以本地方式创建audioLoader和/或MediaPlayer对象(这些对象是shouldOverrideUrlLoading函数的本地对象).因此,一旦退出该功能,当垃圾收集器尝试收集所有未引用的对象时,它将破坏您的对象,然后声音将停止.

You are creating audioLoader and/or MediaPlayer objects in a local way (those objects are locals for the function shouldOverrideUrlLoading). So, once out of the function, when the garbage collector try to collect all non-referenced objects, it will destroy your objects and then the sound will stopped.

尝试将AudioLoader和MediaPlayer对象声明为全局对象:

Try declaring AudioLoader and MediaPlayer objects as global objects:

private AudioLoader audioLoader; 
private MediaPlayer player; 
public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.endsWith(".ogg")){
            audioLoader = new AudioLoader(url);
            audioLoader.start();
            return true;
        }
        else if (url.endsWith(".wav")){
            Uri tempPath = Uri.parse(url);
            player = MediaPlayer.create(interfazWeb, tempPath);
            if (player != null){
                player.start();
            } else {
                Log.e(TAG, "No se puede abrir el audio:" + url);
            }
            return true;
        }
        else if (url.endsWith(".mp3")){
            audioLoader = new AudioLoader(url);
            audioLoader.start();
            return true;
        }else{
            return super.shouldOverrideUrlLoading(view, url);
        } 
}

这篇关于MediaPlayer无法正确播放音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆