首页 > 解决方案 > 为什么我的 C#/ Android 应用程序不播放音频?

问题描述

我是使用 c#/Xamarin 为 android 应用程序开发的新手,我想制作一个简单的应用程序,它有 3 个图像按钮并在单击每个按钮时播放 3 种不同的声音。这是我一次又一次尝试后编写的代码。

该应用程序在模拟器中运行,我单击按钮但没有声音。

using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Android.Media;

namespace The_Useless_App_1._0
{
    [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
    public class MainActivity : Activity
    {

        MediaPlayer chaos_player,hallo_player,oink_player;


        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);


            void ChaosPlayer(MediaPlayer chaos_player)
            {
                chaos_player = MediaPlayer.Create(this, Resource.Raw.Chaos);
                chaos_player.Start();
                return;
            }

            void HalloPlayer (MediaPlayer hallo_player)
            {
                hallo_player = MediaPlayer.Create(this, Resource.Raw.Hallo);
                hallo_player.Start();
                return;
            }

            void OinkPlayer(MediaPlayer oink_player)
            {
                oink_player = MediaPlayer.Create(this, Resource.Raw.Oink);
                oink_player.Start();
                return;
            }

            FindViewById<ImageButton>(Resource.Id.Chaos).Click += (sender, e) => ChaosPlayer(chaos_player);
            FindViewById<ImageButton>(Resource.Id.Hallo).Click += (sender, e) => HalloPlayer(hallo_player);
            FindViewById<ImageButton>(Resource.Id.Oink).Click  += (sender, e) =>  OinkPlayer(oink_player);
        }
    }
}

标签: c#androidxamarin

解决方案


首先,请确保您的文件放在正确的文件夹中,然后音频文件的大小不能太大,您可以尝试更改一个小于 1M 的音频文件,然后进行测试。

我写了一个关于播放音频的demo,你可以参考一下。

首先创建三个按钮(播放、暂停、停止)。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/Chaos"
    android:text="Play Chaos"
/>

<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/StopChaos"
    android:layout_below="@id/Chaos"
    android:text="Stop Chaos"
/>
<Button 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/PauseChaos"
    android:text="Pause Chaos"
    android:layout_below="@id/StopChaos"
/>
</RelativeLayout>

然后,达到mediaplayer;

   [Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    MediaPlayer chaos_player;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        SetContentView(Resource.Layout.activity_main);


        Button Chaos = FindViewById<Button>(Resource.Id.Chaos);
        Button StopChaos = FindViewById<Button>(Resource.Id.StopChaos);
        Button PauseChaos = FindViewById<Button>(Resource.Id.PauseChaos);

        //before click the button , you could instantiate MediaPlayer outside the click method;
        chaos_player = MediaPlayer.Create(this, Resource.Raw.Chaos);
        Chaos.Click += (e, o) =>
        {
            //if you click the button ,then call different method, do not instantiate MediaPlayer in here
            chaos_player.Start();

        };

        StopChaos.Click += (e, o) =>
        {
            chaos_player.Stop();

        };
        PauseChaos.Click += (e, o) =>
        {
            chaos_player.Pause();

        };

    }

    protected override void OnStop()
    {
        base.OnStop();
        //if you stop use MediaPlayer,please release it.
        chaos_player.Release();
    }
}

有一个关于如何在 xamarin android 中播放音频文件的链接,您可以参考它。 https://www.youtube.com/watch?v=AdUSSx6dsOs


推荐阅读