我是使用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);
}
}
}
答案 0 :(得分:0)
使用音频文件-您如何命名它们以及将它们放置在哪里?使用android,您不必在文件名中包含任何大写字母,并且它们必须位于正确的文件夹中(我相信它位于resources文件夹中)
答案 1 :(得分:0)
首先,请确保将文件放在正确的文件夹中,然后音频文件的大小不要太大,您可以尝试更改小于1M的音频文件,然后进行测试。
我写了一个有关Play Audio的演示,您可以参考它。
首先创建三个按钮(播放,暂停,停止)。
<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