Tag Archives: file

Play audio in an Android app with Xamarin

This tutorial explains how to play audio in an Android app made in Xamarin. The audio can be played when a screen loads or when an event occurs such as a button click or change in a variable’s value. For this tutorial, we will play a sound when a button is tapped.


Step 1

Start by creating a new app solution in Xamarin (click File > New Solution). Select Android App as the project template. Click Next.


Give the new app a name. Then click Next.


Confirm the solution and project name, Make sure you specify a folder to store your project files in and then click Create.


Step 2

Open the Main.axml file (in Resources > layout). Delete the button already there. Save the file.


Step 3 

Open the MainActivity.cs file and delete the highlighted lines of code (these lines for the button are not needed anymore as we won’t be using the default button).


Step 4 

Right-click on the Resources folder. Select Add > New Folder.


Give the new folder the name ‘raw’ and then press Enter.


Step 5

Select the new ‘raw’ folder and right-click it. Then click Add > Add files from folder.


Step 6

Navigate to the folder which contains the audio file(s) you wish to use in your app. Click Open.


Step 7

Select the audio files you wish to include in the project (for this example, we will use an MP3 file called mysound.mp3). Click OK.


Then select Copy the file to the directory and click OK. This file will be copied to the project’s raw folder. You should then be able to see the audio file in the raw folder from the Solution panel.


Step 8

Open the Main.axml file again and drag a Button element from the Toolbox onto the phone screen. Change the button’s Id value to @+id/playButton. Then change its Text value to Play Sound. Save this file.


Step 9

Open the MainActivity.cs file again. Add the following highlighted line of code to the top section of the existing code. This will give us access to Android.Media.

Click the image to see a larger version.

Step 10

Add the following two highlighted lines of code to the MainActivity.cs file to define the MediaPlayer element and to include the audio file in the code. Replace mysound with the name of your audio file (there is no need to include the .mp3 extension).

Click the image to see a larger version.

Step 11

Now add the following two highlighted lines of code to define the Button element and access the playButton button from the screen.

Click the image to see a larger version.

Step 12

Now add a click event for the button inside the OnCreate method. As you type playButton.Click+= and press the spacebar key you will see a list of options. You should see the option to create the PlayButton_Click method (if you don’t, start typing its name until it shows up – as shown below). Double-click this option from the list to create a new method.

Click the image to see a larger version.

Step 13

Inside the new PlayButton_Click method add the line _player.Start(); – this will play the sound file when the button is clicked/tapped by the user. You can use this line anywhere in your app to play the sound (not just on a click event).

Click the image to see a larger version.

That’s it! Now go and test your app. Hint: You can also add a button that runs the line of code _player.Stop(); to stop the audio file playing.


Publishing your Android app

This tutorial explains how to publish your app and create an .APK file that can be installed on an Android phone. For this tutorial, we will be using the CookieClicker app. You might also want to change the app icon and details before publishing your app.

Watch the video below or scroll down to read the step-by-step instructions.


Step 1

To get started, open the solution that you wish to publish in Xamarin. You will need to create an Android Signing Key before you can publish your app. To do this, click on the Xamarin Studio Community menu and select Preferences (this might be on the File menu in Windows).


Step 2

In the Preferences dialog, click on Android Signing Keys (under Publishing) and click on Create New Key.


Step 3

Give your new signing certificate an alias (name) and create a password (at least 6 characters long). Then select the validity length (how many years the certificate will be valid for) and fill in at least of the text boxes with something (eg. just provide your name or your city name). Click on OK to continue.


You should then see your new signing key/certificate in the list of Android Signing Keys. Click on OK to exit the Preferences dialog.


Step 4

Select the Project menu and change your Active Configuration to Release. This will allow you to publish your app (you can change this back to Debug after you have published your app so you can return back to coding and testing your apps).


Step 5

Select the Build menu and then click Archive for Publishing.


Step 6

Now click on Sign and Distribute.


Then select Ad-Hoc to save the app file (the .apk file) to your computer. Click on Next.


Step 7

Select your signing key from the list and click Next.


Then confirm you are happy with the app details and file size and click Publish.


Step 8

Lastly, choose where to save your .apk file and click on Save. You can now share this file online or copy it to your phone (either by USB cable or email). You can also upload it to the Google Play store for other people to download.


Step 9

After you have published your app, remember to change the project’s active configuration back to Debug by clicking on the Project menu > Active Configuration > Debug.

Important note

If you decide to upload your app to the Google Play store you must make sure that you do not violate any copyright laws (eg. check that you are not using other people’s images or sound files and that you app is not the same as an existing app). Also, make sure you have your parents’ permission before uploading any apps to the Google Play store.

How to use an image file as a texture in Maya

This video explains how to use an image file (eg. a JPEG) as a texture for an object in Maya. You can use an image to texture an entire shape or plane, or just selected faces of a shape. Watch the video to find out how. You can also download printed instructions from here.

Note: To apply a texture to just selected faces of a shape rather than the entire shape, right-click the shape and click on Face to enter Face Mode. You should then be able to select individual faces and right-click to assign a material to selected faces. This allows you to use different textures for different sides of a shape.

How to export your animation to a movie file

So, you’ve finished your animation and it’s awesome?! The next thing you’ll want to do is export it to a video file so that you can share it with the world! This video tutorial explains how to export an Adobe Flash animation to a video file (such as AVI, MOV, SWF or GIF) which can be shared with friends, embedded in a webpage, or uploaded to a social networking or video sharing site. You can view the video below or click here to watch it on YouTube.

Why not share your animation here? You can post a comment with a link to your video below.